﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;
using Bymed.SpineScan.DataModel;
using Bymed.SpineScan.WPF.Shared;
using Bymed.SpineScan.WPF.ViewModel;
using Bymed.SpineScan.WPF.ViewModel.TestItemViewModel;

namespace Bymed.SpineScan.WPF
{
    /// <summary>
    /// Interaction logic for TestSummaryReportPage.xaml
    /// </summary>
    public partial class TestSummaryPage : PageBase
    {
        public TestSummaryPage()
        {
            InitializeComponent();
            InitializePageUnloadStoryboard();
        }

        protected override void RegisterMessages()
        {
            base.RegisterMessages();

            RegisterSingleTestDetailMessage();
            RegisterShowTestSummaryReportMessage();
            RegisterFinishTestMessage();
        }

        /// <summary>
        /// Register the ShowSingleTestReportMessage from TestSummaryViewModel to show the single test result report.
        /// </summary>
        private void RegisterSingleTestDetailMessage()
        {
            Messenger.Default.Register<SingleTestItemViewModel>(this, AllTestResultsViewModel.ShowSingleTestDetailMessageToken, p =>
            {
                try
                {
                    var singleTestDetailWindow = new SingleTestDetailWindow();
                    var singleTestDetailViewModel = singleTestDetailWindow.DataContext as SingleTestDetailViewModel;
                    singleTestDetailViewModel.PatientBasicInfoDisplayViewModel = (DataContext as TestSummaryViewModel).PatientBasicInfoDisplayViewModel;
                    singleTestDetailViewModel.LoadHistoryData(p);
                    singleTestDetailWindow.ShowDialog();
                }
                catch (Exception ex)
                {
                    MyViewModelBase.RequestDisplayErrorMessage(ex.Message);
                }
            });
        }

        /// <summary>
        /// Register the ShowTestSummaryReportMessage from TestSummaryViewModel to show the test summary result report.
        /// </summary>
        private void RegisterShowTestSummaryReportMessage()
        {
            Messenger.Default.Register<TestSummaryViewModel>(this, TestSummaryViewModel.ShowTestSummaryReportMessageToken, p =>
            {
                try
                {
                    var reportWindow = new TestReportWindow();
                    var testReportViewModel = reportWindow.DataContext as TestReportViewModel;
                    testReportViewModel.DignosticMessageList = p.SelectedDiagnosticMessageList;
                    testReportViewModel.AttachedDiagnosticMessage = p.AttachedDiagnosticMessage;
                    testReportViewModel.ResetProperties();
                    ViewModelHelper.CopyTestItemAndAllRelatedInfoReference(p, testReportViewModel);

                    reportWindow.ShowDialog();
                }
                catch (Exception ex)
                {
                    MyViewModelBase.RequestDisplayErrorMessage(ex.Message);
                }
            });
        }

        /// <summary>
        /// Register the FinishTestMessage from TestSummaryViewModel, go back to home page and finish current test.
        /// </summary>
        private void RegisterFinishTestMessage()
        {
            Messenger.Default.Register<TestSummaryViewModel>(this, TestSummaryViewModel.FinishTestMessageToken, p =>
            {
                GoBackToHomePage();
            });
        }

        private void ButtonBack_Click(object sender, RoutedEventArgs e)
        {
            GoToPage(new StartTestPage(), false);
        }
    }
}
